package cl.jsoftlabs.gwt.soccer.client.ui.pais;

import java.util.ArrayList;
import java.util.List;

import cl.jsoftlabs.gwt.soccer.client.AppConstantes;
import cl.jsoftlabs.gwt.soccer.client.bean.PaisVO;
import cl.jsoftlabs.gwt.soccer.client.services.PaisService;
import cl.jsoftlabs.gwt.soccer.client.services.PaisServiceAsync;
import cl.jsoftlabs.gwt.soccer.client.util.BeanModelUtil;

import com.extjs.gxt.ui.client.Registry;
import com.extjs.gxt.ui.client.Style.SelectionMode;
import com.extjs.gxt.ui.client.data.BeanModel;
import com.extjs.gxt.ui.client.data.ModelData;
import com.extjs.gxt.ui.client.event.ButtonEvent;
import com.extjs.gxt.ui.client.event.SelectionChangedEvent;
import com.extjs.gxt.ui.client.event.SelectionChangedListener;
import com.extjs.gxt.ui.client.event.SelectionListener;
import com.extjs.gxt.ui.client.store.ListStore;
import com.extjs.gxt.ui.client.widget.ContentPanel;
import com.extjs.gxt.ui.client.widget.Info;
import com.extjs.gxt.ui.client.widget.LayoutContainer;
import com.extjs.gxt.ui.client.widget.Status;
import com.extjs.gxt.ui.client.widget.button.Button;
import com.extjs.gxt.ui.client.widget.button.ButtonBar;
import com.extjs.gxt.ui.client.widget.grid.ColumnConfig;
import com.extjs.gxt.ui.client.widget.grid.ColumnData;
import com.extjs.gxt.ui.client.widget.grid.ColumnModel;
import com.extjs.gxt.ui.client.widget.grid.Grid;
import com.extjs.gxt.ui.client.widget.grid.GridCellRenderer;
import com.extjs.gxt.ui.client.widget.layout.FitLayout;
import com.google.gwt.user.client.Element;
import com.google.gwt.user.client.rpc.AsyncCallback;

public class PaisesGrid extends LayoutContainer {
	
	public static final String ITEM_ID = "grid-paises";
	
	public PaisesGrid() {
		status.setBox(true);
		status.setIconStyle("face-monkey");
		setItemId(ITEM_ID);
	}

	// gwt service
	final PaisServiceAsync rpcService = PaisService.Util.getInstance();

	PaisVO paisVO = new PaisVO();
	
	private final ButtonBar buttonBar = new ButtonBar();
	private final Status status = new Status();
	
	@SuppressWarnings("unused")
	private Integer idPaisSeleccionado = 0;

	@Override
	protected void onRender(Element parent, int index) {
		super.onRender(parent, index);
		setStyleAttribute("padding", "5px");
		
		ContentPanel cp = new ContentPanel();  
		  
	    cp.setHeading("Paises");  
	    cp.setFrame(true);  
	    cp.setSize(600, 500);

		Grid<BeanModel> grid = buildGrid();
		
		
  
    
	    cp.setLayout(new FitLayout());
	    
	    cp.add(grid);
	    	    
	    cp.setCollapsible(true);
	    
	    cp.setTopComponent(buttonBar);
	    
	    cp.setBottomComponent(status);
 
	    add(cp); 
	    cp.setSize("600", "500");

	}
	

	GridCellRenderer<ModelData> itemsRenderer = new GridCellRenderer<ModelData>() {
		@Override
		public Object render(ModelData model, String property,
				ColumnData config, int rowIndex, int colIndex,
				ListStore<ModelData> store, Grid<ModelData> grid) {
			PaisVO bean = new PaisVO();
			bean = BeanModelUtil.get().toPaisBean(model);
			return "<img src=\"img/banderas/"+bean.getCodDos()+".png\" alt=\""+bean.getNombrePais()+"\" />";
		}
	};
	
	private GridCellRenderer<ModelData> renderHabilitacionPais = new GridCellRenderer<ModelData>() {

		@Override
		public Object render(final ModelData model, String property,
				ColumnData config, int rowIndex, int colIndex,
				ListStore<ModelData> store, Grid<ModelData> grid) {
			Button btnHabilitarPais = new Button("Desabilitar");
			btnHabilitarPais.addSelectionListener(new SelectionListener<ButtonEvent>() {
				
				@Override
				public void componentSelected(ButtonEvent ce) {
					Info.display("Presiona Boton", (String) model.get("nombrePais"));
					PaisVO bean = new PaisVO();
					bean = BeanModelUtil.get().toPaisBean(model);
					if (bean.getBooCodVisible() == AppConstantes.VERDADERO) {
						bean.setBooCodVisible(AppConstantes.FALSO);
					}else if(bean.getBooCodVisible() == AppConstantes.FALSO){
						bean.setBooCodVisible(AppConstantes.VERDADERO);
					}
					actualizarRegistro(bean);
				}
			});
			return btnHabilitarPais;
		}
	};
	
	private void actualizarRegistro(PaisVO bean) {
		PaisService.Util.getInstance().actualizarRegistro(bean, new AsyncCallback<Integer>() {
			
			@Override
			public void onSuccess(Integer codRespuesta) {
				Info.display("Actualizar Registro", " "+codRespuesta);
				
			}
			
			@Override
			public void onFailure(Throwable arg0) {
				// TODO Auto-generated method stub
				
			}
		});	
	}

	private Grid<BeanModel> buildGrid() {

		ListStore<BeanModel> store = Registry.get(AppConstantes.PAIS_STORE_LIST);

		List<ColumnConfig> columns = new ArrayList<ColumnConfig>();
		
		ColumnConfig cfPais = new ColumnConfig();
		cfPais.setRenderer(itemsRenderer);
		cfPais.setHeader("Bandera");
		cfPais.setWidth(30);
		
		
		columns.add(cfPais);
		
		columns.add(new ColumnConfig("codDos", "Cod 2", 30));
		columns.add(new ColumnConfig("codTres", "Cod 3", 60));
		columns.add(new ColumnConfig("nombrePais", "Nombre", 160));
		ColumnConfig cfHabilitar = new ColumnConfig();
		cfHabilitar.setRenderer(renderHabilitacionPais);
		cfHabilitar.setHeader("Estado");
		cfHabilitar.setWidth(50);
		columns.add(cfHabilitar);
		ColumnModel cm = new ColumnModel(columns);

		Grid<BeanModel> tabla = new Grid<BeanModel>(store, cm);

		tabla.setAutoExpandColumn("nombrePais");
		tabla.getSelectionModel().setSelectionMode(SelectionMode.SINGLE);
		
		tabla.getSelectionModel().addSelectionChangedListener(new SelectionChangedListener<BeanModel>() {
			@Override
			public void selectionChanged(SelectionChangedEvent<BeanModel> se) {
				PaisVO bean = new PaisVO();
				bean = BeanModelUtil.get().toPaisBean(se.getSelection().get(0));
				status.setIconStyle("face-monkey");
				status.setText("<img src=\"img/blank.gif\" class=\"flag flag-"+bean.getCodDos()+"\" alt=\""+bean.getNombrePais()+"\" />");
			}
		});
		return tabla;
	}

}
